package com.jinyu.jyly.ui.mine.address;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AddressList;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.ui.mine.address.change_address.ChangeAddressActivity;
import com.jinyu.jyly.utils.AES;
import com.jinyu.jyly.utils.DisplayCutoutUtil;


import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;


/**
 * 更新收货地址
 */
public class ShipUpdateAddressActivity extends BaseActivity<ShipAddressUpdatePresenter> implements ShipAddressUpdateContract.View, View.OnClickListener {

    @BindView(R.id.fake_status_bar)
    View fake_status_bar;
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.et_mobile)
    EditText et_mobile;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.et_detail_address)
    EditText et_detail_address;
    @BindView(R.id.cb_normal_address)
    CheckBox cb_normal_address;
    @BindView(R.id.btn_sub)
    Button btn_sub;
    private ShipAddressUpdatePresenter mPresenter;
    private static final int CHANGE_AREA = 1;// 结果

    private int mId;
    private String mArea;
    private String mCity;
    private String mProvince;
    private String mPca;
    private AddressList mInfoBean;


    @Override
    public int getLayout() {
        return R.layout.activity_ship_update_address;
    }

    @Override
    protected ShipAddressUpdatePresenter getPresenter() {
        if (mPresenter == null) {
            mPresenter = new ShipAddressUpdatePresenter(this, this);
        }
        return mPresenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
        tv_address.setOnClickListener(this);
        btn_sub.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        fitterScreen();
        initIntent();
    }

    private void initIntent() {
        Intent intent = getIntent();
        mInfoBean = (AddressList) intent.getSerializableExtra("infoBean");
        if (mInfoBean != null) {
            mId = mInfoBean.getId();
            String address = mInfoBean.getAddress();
            mArea = mInfoBean.getArea();
            mCity = mInfoBean.getCity();
            mProvince = mInfoBean.getProvince();
            mPca = mInfoBean.getPca();
            et_detail_address.setText(mPca);

            String name = mInfoBean.getName();
            String phone = mInfoBean.getPhone();
            int is_default = mInfoBean.getIs_default();

            if ("1".equals(String.valueOf(is_default))) {
                cb_normal_address.setChecked(true);
            } else {
                cb_normal_address.setChecked(false);
            }

            tv_address.setText(mProvince + mCity + mArea);
            et_name.setText(name);
            et_mobile.setText(phone);
            et_detail_address.setText(address);

        }
    }

    /**
     * 适配刘海
     */
    private void fitterScreen() {
        if (mIsNeed) {
            int pH = DisplayCutoutUtil.getStatusBarHeight(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.height = pH;
            fake_status_bar.setLayoutParams(params);
        }
    }


    /**
     * 添加收获地址
     *
     * @param info
     */
    @Override
    public void onAddAddress(AllInfoBean info) {
        if (mTipDialog != null) {
            mTipDialog.dismiss();

        }
        switch (info.getStatus()) {
            case 200:
                String data = info.getData();
//                    String result = AES.decrypt(data);
                showToast(info.getMsg());
                finish();
                break;
            case 422:
                showToast(info.getMsg());
                break;
            default:
                break;
        }
    }


    @Override
    public void onFailer(Throwable throwable) {
        if (mTipDialog != null) {
            mTipDialog.dismiss();

        }
    }

    public int isChecked = 0;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sub:
                //提交
                String name = et_name.getText().toString().trim();
                String mobile = et_mobile.getText().toString().trim();
                String detailAddress = et_detail_address.getText().toString().trim();
                boolean checked = cb_normal_address.isChecked();
                if (TextUtils.isEmpty(name)) {
                    showToast("名称不能为空");
                    return;
                }
                if (TextUtils.isEmpty(mobile)) {
                    showToast("手机号不能为空");
                    return;
                }

                if (TextUtils.isEmpty(detailAddress)) {
                    showToast("详细地址不能为空");
                    return;
                }
                if (checked) {
                    isChecked = 1;
                } else {
                    isChecked = 0;
                }

                if (mTipDialog != null) {
                    mTipDialog.show();
                }

                try {
                    JSONObject jsonObject = new JSONObject();
                    if (!TextUtils.isEmpty(String.valueOf(mId))) {
                        jsonObject.put("id", mId);
                    }
                    jsonObject.put("name", name);
                    jsonObject.put("phone", mobile);
                    jsonObject.put("province", mProvince);
                    jsonObject.put("city", mCity);
                    jsonObject.put("area", mArea);
                    jsonObject.put("address",detailAddress);
                    jsonObject.put("default", isChecked);
                    String pamam = AES.encryptToHexString(jsonObject.toString());
                    mPresenter.setAddAddress(pamam);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                break;
            case R.id.tv_address:
                //选择省市区
                Intent mChangeIntent = new Intent(this, ChangeAddressActivity.class);
                startActivityForResult(mChangeIntent,CHANGE_AREA);
                break;
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CHANGE_AREA:
                if (data != null) {
                    mProvince = data.getStringExtra("province");
                    mCity = data.getStringExtra("city");
                    mArea = data.getStringExtra("area");

                    tv_address.setText(mProvince + "·" + mCity + "·" + mArea);
                }
                break;
            default:
                break;
        }
    }
}
